﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Web;

namespace DAL
{
    public class DonDatHangQuery : DataProvide
    {
        public List<DonDatHangDAL> LayDSDonHang()
        {
            List<DonDatHangDAL> dsDonHang = new List<DonDatHangDAL>();
            SqlConnection chuoiKetNoi = null;
            try
            {
                base.Connect();
                chuoiKetNoi = connection;
                string sqlString = String.Format(@"SELECT * FROM DonDatHang WHERE TrangThaiGiaoHang = '" + 1 + "'");

                SqlDataReader dr = ExecuteReader(sqlString);
                while (dr.Read())
                {
                    DonDatHangDAL donHang = (DonDatHangDAL)GetDataRowFromDataReader(dr);
                    dsDonHang.Add(donHang);

                }
            }/*
            catch
            {
                return null;
            }*/
            finally
            {
                if (chuoiKetNoi != null && chuoiKetNoi.State == System.Data.ConnectionState.Open)
                    chuoiKetNoi.Close();
            }
            return dsDonHang;
        }
        public List<DonDatHangDAL> LayDSDonHangTheoTinhTrang(bool ts)
        {
            List<DonDatHangDAL> dsDonHang = new List<DonDatHangDAL>();
            SqlConnection chuoiKetNoi = null;
            try
            {
                base.Connect();
                chuoiKetNoi = connection;
                string sqlString = String.Format(@"SELECT * FROM DonDatHang WHERE TrangThaiGiaoHang = '" + ts + "'");

                SqlDataReader dr = ExecuteReader(sqlString);
                while (dr.Read())
                {
                    DonDatHangDAL donHang = (DonDatHangDAL)GetDataRowFromDataReader(dr);
                    dsDonHang.Add(donHang);

                }
            }/*
            catch
            {
                return null;
            }*/
            finally
            {
                if (chuoiKetNoi != null && chuoiKetNoi.State == System.Data.ConnectionState.Open)
                    chuoiKetNoi.Close();
            }
            return dsDonHang;
        }

        protected override object GetDataRowFromDataReader(SqlDataReader dr)
        {
            DonDatHangDAL donHang = new DonDatHangDAL();
            donHang.MaDonDatHang = dr.GetString(0);
            if (!dr.IsDBNull(1))
                donHang.TaiKhoan = dr.GetString(1);
            //if (!dr.IsDBNull(2))
            // donHang.NgayDat = dr.GetValue(2);
            if (!dr.IsDBNull(3))
                donHang.TongTien = float.Parse(dr.GetDouble(3).ToString());
            if (!dr.IsDBNull(4))
                donHang.TrangThaiGiaoHang = dr.GetBoolean(4);

            return (object)donHang;

        }
    }
}
